package com.tao.encap;

import java.sql.SQLOutput;

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("陶镓妮");
        person.setAge(19);
        person.setSalary(10000);
        System.out.println(person.info());
        System.out.println(person.getSalary());

        //使用构造器指定属性
        Person smith = new Person("smith",18,20000);
        System.out.println("==========smith==========");
        System.out.println(smith.info());
    }
}
class Person{
    public String name;
    private int age;//私有
    private double salary;//私有

    //构造器
    public Person() {
    }
    public Person(String name, int age, double salary) {
        //this.name = name;
        //this.age = age;
        //this.salary = salary;
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() >= 2 && name.length() <= 6) {
            this.name = name;
        }else{
            System.out.println("姓名设置不对，需要在（2-6）之间，给默认名字为无名");
            this.name = "无名";
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age >=1 && age <=120){
            this.age = age;
        }else{
            System.out.println("年龄设置不对，需要在（1-120）之间，给默认年龄为20");
            this.age = 20;
        }
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String info(){
        return "name=" + name + " " + "age=" + age + " " +"salary=" + salary + " " ;
    }

}